package com.example.pharmacy.controller;

import com.example.pharmacy.model.OrderItem;
import com.example.pharmacy.model.Result;
import com.example.pharmacy.service.OrderItemService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2024-12-22
 * Time: 19:17
 */
@RestController
@RequestMapping("/orderitem")
public class OrderItemController {
    @Autowired
    private OrderItemService orderItemService;

    /**
     * 只能修改药品数量, 不能修改药品id和订单id
     * @param orderItem
     * @return
     */
    @RequestMapping("/updateOrderItem")
    public Result updateOrderItem(OrderItem orderItem) {
        if (orderItem == null || orderItem.getOrderItemId() == null) {
            return Result.fail("订单项信息有误");
        }
        Integer ret = orderItemService.updateOrderItem(orderItem);
        if (ret == null) {
            return Result.fail("更新订单项失败");
        }
        if (ret == 0) {
            return Result.fail("该药品库存不足");
        }
        return Result.success("");
    }
    @RequestMapping("/addOrderItems")
    public Result addOrderItems(List<OrderItem>orderItems) {
        if (orderItems == null || orderItems.size() == 0) {
            return Result.fail("添加的订单项信息有误");
        }
        Integer ret = orderItemService.addOrderItems(orderItems);
        if (ret == null) {
            return Result.fail("增加订单项失败");
        }
        if (ret == 0) {
            return Result.fail("该药品库存不足");
        }
        return Result.success("");
    }

}
